package com.yiwei.hdb.view;

import android.animation.ArgbEvaluator;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.yiwei.hdb.R;

/**
 * Created by xsooy-pc on 2017/7/14.
 */

public class MyPagerTransformer implements ViewPager.PageTransformer, ViewPager.OnPageChangeListener {
    private int pageIndex;

    /**
     * 此方法滑动过程中每个view都会回调 @param view     当前view @param position 滑动时数值范围（-1,1）当前滑动位置
     */
    @Override
    public void transformPage(View view, float position) { /*获取到ViewPager*/
        View v = (View) view.getParent();
        int color0 = R.color.refresh_color_bule;
        int color1 = R.color.refresh_color_green;
        int color2 = R.color.refresh_color_orange;
        int tag = (int) view.getTag();
        int color = color0;
        //ArgbEvaluator 用于颜色渐变
        ArgbEvaluator argbEvaluator = new ArgbEvaluator();
        //为了填坑所以传了一个参数tag
        if (tag == pageIndex) {
            switch (pageIndex) {
                case 0:
                    color = (int) argbEvaluator.evaluate(Math.abs(position), color0, color1);
                    break;
                case 1:
                    color = (int) argbEvaluator.evaluate(Math.abs(position), color1, color2);
                    break;
                case 2:
                    color = (int) argbEvaluator.evaluate(Math.abs(position), color2, color1);
                    break;
                default:
                    break;
            }
            v.setBackgroundResource(color);
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        pageIndex = position;

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
